<template>
    <template v-if="local">
        <svg aria-hidden="true" :width="attrs?.width??attrs.height??'1em'" :height="attrs?.height??attrs.width??'1em'" v-bind="attrs" >
            <use :xlink:href="symbolId" fill="currentColor" />
        </svg>
    </template>
    <template v-else>
        <Icon v-if="icon" :icon="icon" v-bind="attrs" />
    </template>
</template>

<script setup>
import { computed, useAttrs } from 'vue'
import { Icon } from '@iconify/vue'

defineOptions({ name: 'SvgIcon' })

/**
 * 图标组件
 * - 支持iconify和本地svg图标
 * - 同时传递了icon和localIcon，localIcon会优先渲染
 */
const props = defineProps({
    /** 图标名称 */
    icon: String,
    /** 是否使用本地svg */
    local: Boolean,

})

const attrs = useAttrs();

const symbolId = computed(() => {

    const defaultLocalIcon = 'no-icon'

    const icon = props.icon.split(":").join('--')

    return `#icon-${icon}`
})

</script>

<style scoped></style>
